#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Debug.h"
#include "mxml.h"
#include "xmlfile_s.h"
#include "file_lock.h"


int xmlfile_init(int nDevType);
int xmlfile_lockfile(const char *xmlfilename);
int xmlfile_unlockfile(const char *xmlfilename);
int xmlfile_gets(const char * xmlfilename, const char * xpath, char * szValue, int nMaxLen, const char * szValueDefault);
int xmlfile_sets(const char * xmlfilename, const char * xpath, char * szValue);
int xmlfile_replace(const char *xmlfile_old, const char *xmlfile_tmp, const char *xmlfile_config);




int xmlfile_init(int nDevType)
{
	return 0;
}

// nvram_get_func2("phonesetting/M_KEY/Key_type1", tmp_key_type1, DIR_PHONESETTING);
// nvram_get_func2("sipsetting/M_DTF/DTMF", tmp,    "sipsettings.xml");
int xmlfile_gets( const char *xmlfilename, const char *xpath_t, char *szValue, int nMaxLen, const char *szValueDefault)
{
    if(NULL == xpath_t || NULL == szValue || nMaxLen <=0 || NULL == xmlfilename || NULL == szValueDefault)
    {
        Safe_Printf("path.null");
        return -1;
    }
    const char *xpath = xpath_t;
    if('/' == xpath_t[0])
        xpath = xpath_t + 1;
    FILE *fp = NULL;
    mxml_node_t *tree = NULL;
    szValue[0] = 0;
    int valueSeted = 0;
    xmlfile_lockfile(xmlfilename);
    do {
        fp = fopen(xmlfilename, "r");
        if(NULL == fp)
        {
            Safe_Printf("error open file:%s", xmlfilename);
            break;
        }
        tree = mxmlLoadFile(NULL, fp, 0); // type_cb);
        mxml_node_t *top = tree;
        if (NULL == tree)
        {
            Safe_Printf("tree=null.file:%s[%s]", xmlfilename, xpath);
            break;
        }
        mxml_node_t *node = mxmlFindPath(top, xpath);
        if (NULL == node)
        {
            Safe_Printf("error.xpath=%s",xpath);
            break;
        }
        const char  *text = mxmlGetText(node, NULL);
        if (NULL == text)
        {
            Safe_Printf("error.xpath=%s", xpath);
            break;
        }
        snprintf(szValue, nMaxLen, "%s", text);
        valueSeted = 1;
    }while(0);
    // if empty value, use default
    if(0 == valueSeted)
    {
        snprintf(szValue, nMaxLen, "%s", szValueDefault);
    }

    if(tree)
    {
        mxmlDelete(tree);
    }
    if(fp)
    {
        fclose(fp);
    }
    xmlfile_unlockfile(xmlfilename);
    return 0;
}

int xmlfile_sets( const char *xmlfilename, const char *xpath_t, char *szValue)
{
    if(NULL == xpath_t || NULL == szValue || NULL == xmlfilename)
    {
        Safe_Printf("path.null");
        return -1;
    }
    const char *xpath = xpath_t;
    if('/' == xpath_t[0])
        xpath = xpath_t + 1;

    FILE *fp = NULL;
    mxml_node_t *tree = NULL;   /* <?xml ... ?> */
    xmlfile_lockfile(xmlfilename);
    do{
        fp = fopen(xmlfilename, "r+");
        if(NULL == fp)
        {
            Safe_Printf("error open file=%s", xmlfilename);
            break;
        }
        tree = mxmlLoadFile(NULL, fp, 0); // type_cb);
        pclose(fp);
        fp = NULL;
        mxml_node_t *top = tree;
        if (NULL == tree)
        {
            Safe_Printf("error tree.null=%s", xmlfilename);
            break;
        }
        mxml_node_t *node = mxmlFindPath(top, xpath);
        if (NULL == node)
        {
            Safe_Printf("error. node=null:%s",xpath);
            break;
        }

        //  set
        if (strlen(szValue) > 0)
        {
            mxmlSetText(node, 0, szValue);
        }
        else
        {
            mxmlSetText(node, 0, " ");
        }
        // save
        fp = fopen(xmlfilename, "w");  
        if(NULL == fp)
        {
            Safe_Printf("error.open=%s", xmlfilename);
            break;
        }
        mxmlSaveFile(tree, fp, 0); //whitespace_cb);
    }while(0);

    if(fp)
        fclose(fp);
    if(tree)
        mxmlDelete(tree);
    xmlfile_unlockfile(xmlfilename);
    return 0;
}

int xmlfile_replace(const char *szXmlfilefrom, const char *xmlfileto_tmp, const char *xmlfileto_config)
{
    // replace xml file
    int rret = -1;

    //   xml  on   /tmp/
    int nfloc_tmp    = file_lock(xmlfileto_tmp);
    rret = rename(szXmlfilefrom, xmlfileto_tmp);
    file_unlock(nfloc_tmp);
    if(rret < 0)
    {
        Safe_Printf("error . rename %s -> %s", szXmlfilefrom, xmlfileto_tmp);
    }

    //   xml  on   /mnt/config/
    int nfloc_config = file_lock(xmlfileto_config);
    rret = rename(szXmlfilefrom, xmlfileto_config);
    file_unlock(nfloc_config);
    if(rret < 0)
    {
        Safe_Printf("error . rename %s -> %s", szXmlfilefrom, xmlfileto_config);
    }
    return rret;
}

int xmlfile_lockfile(const char *xmlfilename)
{
    return 0;
}

int xmlfile_unlockfile(const char *xmlfilename)
{
    return 0;
}



